package physics;

import java.awt.event.ActionEvent;

import common.Math3D;
import common.Point3D;
import common.Position3D;

import physics.Collisions.Collision;
import physics.MovementControl.WalkerMovmentController;
import viewControllers.ViewControllersFactory;
import viewControllers.robot.RobotViewController;
import globals.ObjectFactory;
import globals.ObjectType;

public class GiantRobot extends Robot {

	boolean mustFire = false;
	private boolean dying = false;
	private int ticks = 0;
	private int ticksToDie = 40;
	private int ticksPerKnife = 40;
	
	
	public GiantRobot(){
		this.setMovementControler(new WalkerMovmentController(this));
		this.getMovementControler().setHasMomentum(true);	
		this.getMovementControler().setMovementLimit(0.05);
		this.getMovementControler().setRotationLimit(0.5);
		this.getMovementControler().setRotationFriction(0.02);
		
		hittingRange = 0.8;
		rotAngle = 0.01;
		radius2 = 2;
		maxDamage = 15;
	}
	
	public void fireKnife(Position3D position)
	{
		Knife knife = (Knife)ObjectFactory.createGameObject(ObjectType.KNIFE);
		knife.setPlanet(this.getPlanet());
		knife.getPosition().copy(this.position);
		knife.setFace(Math3D.subPoint(position.getCenter(), this.position.getCenter()));
		planet.pinpointToPlanet(knife);
		knife.jump();
	}
	
	public void impact(Collision c){
		if (dying)
			return;
		
		GameObject bumpedInto;	
		if (c.getObj1().equals(this))
			bumpedInto= c.getObj2();
		else
			bumpedInto = c.getObj1();

		double distance = Math3D.subPoint(bumpedInto.getCenter(), this.getCenter()).norm();  
			
		switch (c.getType()){	
			case GRENADE:
				if ( distance <= hittingRange && ((Grenade)bumpedInto).canHit()){
					((Grenade)bumpedInto).setCanHit(false);
					if (!((Grenade)bumpedInto).disappearing){
						((Grenade)bumpedInto).explode();
					}
					damage++;
					checkDamage();
				}
				break;
			
			case MESSILEROBOT:
				if (((Weapon)bumpedInto).canHit() && inHittingRange(bumpedInto.getCenter())){
					((Weapon)bumpedInto).setCanHit(false);
					damage += 3;
					checkDamage();
				}
				break;	
				
			case AVATARGIANTROBOT:
				rotateTo(bumpedInto);
			
				if (distance <= radius2){
					if (ticks == 0){
						fireKnife(bumpedInto.getPosition());
					}
					if (ticks++ == ticksPerKnife)
						ticks = 0;
				}
				break;
			
		}
	}
	
	



}
